package protocol;
import GameData.*;


public class Query {
	private int transactionNumber;
	private QueryItem numTix;
	private QueryItem lastNumTix;
	private QueryItem homeTeam;
	private QueryItem lastHomeTeam;
	private QueryItem awayTeam;
	private QueryItem lastAwayTeam;
	private QueryItem date;
	private QueryItem lastDate;
	private QueryItem price;
	private QueryItem lastPrice;
	private QueryItem shortConf;
	private QueryItem gameInfo;
	private QueryItem ticketInfo;
	
	public Query()
	{
		transactionNumber = 1;
		numTix = null;
		lastNumTix = null;
		homeTeam = null;
		lastHomeTeam = null;
		awayTeam = null;
		lastAwayTeam = null;
		date = null;
		lastDate = null;
		price = null;
		lastPrice = null;
		shortConf = null;
		gameInfo = null;
		ticketInfo = null;
	}
	
	public TicketAggregate getTicketInfo() {
		if(ticketInfo != null)
			return ticketInfo.getTicketAggregate();
		return null;
	}
	
	public void setTicketInfo(TicketAggregate ta) {
		ticketInfo = new QueryItem(ta, transactionNumber);
		getGameInfo().ticketAggregate = ta;
	}
	
	public Game getGameInfo() {
		if(gameInfo != null)
			return gameInfo.getGame();
		return null;
	}
	
	public void setGameInfo(Game g) {
		gameInfo = new QueryItem(g, transactionNumber);
	}
	
	public String getNumTix() {
		if(numTix != null)
			return numTix.getValue();
		return null;
	}
	
	public void setNumTix(String value)
	{
		lastNumTix = this.numTix;
		this.numTix = new QueryItem(value, transactionNumber);
	}
	
	public boolean numTixIsNew()
	{
		return numTix != null && numTix.getTimeAdded() == transactionNumber - 1;
	}
	
	public String getHomeTeam() {
		if(homeTeam != null)
			return homeTeam.getValue();
		return null;
	}

	public void setHomeTeam(String value) {
		lastHomeTeam = this.homeTeam;
		this.homeTeam = new QueryItem(value, transactionNumber);
	}
	
	public boolean homeTeamIsNew()
	{
		return homeTeam != null && homeTeam.getTimeAdded() == transactionNumber - 1;
	}

	public String getAwayTeam() {
		if(awayTeam != null)
			return awayTeam.getValue();
		return null;
	}

	public void setAwayTeam(String value) {
		lastAwayTeam = this.awayTeam;
		this.awayTeam = new QueryItem(value, transactionNumber);
	}
	
	public boolean awayTeamIsNew()
	{
		return awayTeam != null && awayTeam.getTimeAdded() == transactionNumber - 1;
	}

	public String getDate() {
		if(date != null)
			return date.getValue();
		return null;
	}

	public void setDate(String value) {
		lastDate = this.date;
		this.date = new QueryItem(value, transactionNumber);
	}
	
	public boolean dateIsNew()
	{
		return date != null && date.getTimeAdded() == transactionNumber - 1;
	}

	public String getPrice() {
		if(price != null)
			return price.getValue();
		return null;
	}

	public void setPrice(String value) {
		lastPrice = this.price;
		this.price = new QueryItem(value, transactionNumber);
	}
	
	public boolean priceIsNew()
	{
		return price != null && price.getTimeAdded() == transactionNumber - 1;
	}
	
	public String getShortConf(){
		if(shortConf != null)
			return shortConf.getValue();
		return null;
	}
	
	public void setShortConf(String value){
		if(value == null)
			shortConf = null;
		else
			shortConf = new QueryItem(value, transactionNumber);
	}

	public void endTransaction()
	{
		transactionNumber++;
	}
	
	public void voidTransaction()
	{
		transactionNumber--;
		if(numTix != null && numTix.getTimeAdded() == transactionNumber)
			numTix = lastNumTix;
		if(homeTeam != null && homeTeam.getTimeAdded() == transactionNumber)
			homeTeam = lastHomeTeam;
		if(awayTeam != null && awayTeam.getTimeAdded() == transactionNumber)
			awayTeam = lastAwayTeam;
		if(date != null && date.getTimeAdded() == transactionNumber)
			date = lastDate;
		if(price != null && price.getTimeAdded() == transactionNumber)
			price = lastPrice;
	}
	
	public String getLastTransaction()
	{
		String trans = "";
		trans += "%" + (numTixIsNew() ? numTix.getValue() : "NULL");
		trans += "," + (homeTeamIsNew() ? homeTeam.getValue() : "NULL");
		trans += "," + (awayTeamIsNew() ? awayTeam.getValue() : "NULL");
		trans += "," + (dateIsNew() ? date.getValue() : "NULL");
		trans += "," + (priceIsNew() ? price.getValue() : "NULL");

		return trans;
	}
}
